from flask import Flask, render_template, request, jsonify, redirect, url_for

from services.repaymentmethod import calculate_loane_equal_principal_and_interest, \
    calculate_loane_equal_principal_amount

app = Flask(__name__, template_folder="templates")


@app.route('/')
def hello_world():  # put application's code here
    return render_template("index.html")


# 计算贷款
@app.route('/loan_cl', methods=['POST'])
def houseProvidentFundLoan():  # put application's code here
    post_data = request.form
    dke = post_data["sydke"] if post_data["sydke"] != "" else post_data["gjjdke"]
    dkll = post_data["sydkll"] if post_data["sydkll"] != "" else post_data["gjjdkll"]
    dkqx = post_data["sydkqx"] if post_data["sydkqx"] != "" else post_data["gjjdkqx"]

    if post_data["hkfs"] == '2':
        # 等额本金
        monthly_payment_list, total_payment, interest_paid = calculate_loane_equal_principal_amount(int(dke),
                                                                                                    float(dkll) / 100,
                                                                                                    int(dkqx))
        datajson = {"monthlypaymentlist": monthly_payment_list, "totalpayment": interest_paid,
                    "interestpaid": interest_paid}
        data = {"data": datajson, "msg": "success"}
        return jsonify(data)
    else:
        # 等额本息
        monthly_payment, total_payment, interest_paid = calculate_loane_equal_principal_and_interest(int(dke),
                                                                                                     float(dkll) / 100,
                                                                                                     int(dkqx))
        datajson = {"monthlypayment": round(monthly_payment, 2), "totalpayment": round(total_payment, 2),
                    "interestpaid": round(interest_paid, 2)}
        data = {"data": datajson, "msg": "success"}
        return jsonify(data)




@app.route("/sydkjsq")
def sydkjsq():
    return render_template("sydkjsq.html")


@app.route("/gjjdkjsq")
def gjjdkjsq():
    return render_template("gjjdkjsq.html")


@app.route("/zhdkjsq")
def zhdkjsq():
    return render_template("zhdkjsq.html")


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8089, debug=True)
